﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CC.UI
{
    public delegate void ClearSlidersStatus();
    public partial class SliderGroup : UserControl
    {
        //定义数值修改事件
        [Category("操作"), Browsable(true), Description("滑动条数量")]
        public event EventHandler<ValueChangedEventArgs> ValueChanged;
        [Browsable(false), Description("滑动条数量")]
        public int NumberOfSliders { get { return mSliders.Count; } }
        private List<CcSliderNoRowTitle> mSliders = new List<CcSliderNoRowTitle>();
       
        [Browsable(false), Description("所有滑动条的当前值")]
        /// <summary>
        /// 每个Slider的当前值
        /// </summary>
        public float[] Values { get
            {
                float[] values = new float[mSliders.Count];
                for (int i = 0; i < mSliders.Count; i++)
                {
                    values[i] = mSliders[i].Value;
                }
                return values;
            } }
        public SliderGroup()
        {
            InitializeComponent();
            AddSlider(new CcSliderNoRowTitle("SLD1", 0));
            AddSlider(new CcSliderNoRowTitle("SLD2", 0));
            AddSlider(new CcSliderNoRowTitle("SLD3", 0));
            AddSlider(new CcSliderNoRowTitle("SLD4", 0));
            this.BorderStyle = BorderStyle.FixedSingle;
            foreach (var sld in mSliders)
            {
                sld.ClearStatus = ClearSlidersStaus;
                sld.ValueChanged += GetCurrentSlider;
                
            }
           

        }
        private void GetCurrentSlider(object sender,EventArgs e)
        {

            var  sld = mSliders.FirstOrDefault(s => s.IsLastInvoked);
            //触发数值改变事件 2021-2-14 OK
            ValueChangedEventArgs evt = new ValueChangedEventArgs();
            evt.CurrentSlider = sld;
            Invoke(ValueChanged, sender, evt);
        }

        private void SliderGroup_Load(object sender, EventArgs e)
        {
            
        }
        /// <summary>
        /// 获取指定滑条的当前值
        /// </summary>
        /// <param name="indexOfSlider"></param>
        /// <returns></returns>
        public float GetValueAt(int indexOfSlider)
        {
            if (indexOfSlider < mSliders.Count)
                return mSliders[indexOfSlider].Value;
            else
                return float.NaN;
        }
        /// <summary>
        /// 以名字来读取滑动条的当前值
        /// </summary>
        /// <param name="slidername"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool TryGetValueOf(string sliderName, out float value)
        {
            bool res = false;
            value = float.NaN;
            var slder = mSliders.FirstOrDefault(s => s.Name == sliderName);
            res = slder != null;
            if(res)
            {
                value = slder.Value;
            }
            return res;
        }
        public void SetMaximumAt(int indexOfSlider,double value)
        {
            if(indexOfSlider<mSliders.Count)
            {
                mSliders[indexOfSlider].Maximum =(float) value;
            }
        }
        public void SetMaximumFor(string sliderName, double value)
        {
            var slder = mSliders.FirstOrDefault(s => s.Name == sliderName);
            if(slder!=null)
            {
                slder.Maximum = (float)value;
            }
        }
        public void SetMinimumAt(int indexOfSlider, double value)
        {
            if (indexOfSlider < mSliders.Count)
            {
                mSliders[indexOfSlider].Minimum = (float)value;
            }
        }
        public void SetMminimumFor(string sliderName, double value)
        {
            var slder = mSliders.FirstOrDefault(s => s.Name == sliderName);
            if (slder != null)
            {
                slder.Minimum = (float)value;
            }
        }
        public void SetStepAt(int indexOfSlider, double value)
        {
            if (indexOfSlider < mSliders.Count)
            {
                mSliders[indexOfSlider].Step = (float)value;
            }
        }
        public void SetStepFor(string sliderName, double value)
        {
            var slder = mSliders.FirstOrDefault(s => s.Name == sliderName);
            if (slder != null)
            {
                slder.Step = (float)value;
            }
        }
        public void ClearSliders()
        {
            int count = Controls.Count;
            if (count > 4)//前四个是label
            {
                
                for (int i = 0; i < count - 4; i++)
                {
                    Controls.RemoveAt(Controls.Count - 1);
                }
            }
            
            mSliders.Clear();
        }

        /// <summary>
        /// 重置滑条数，原有的全部会被清掉，然后新生成
        /// </summary>
        /// <param name="count">大于等于0</param>
        public void ResetToSliders(int count)
        {
            if (count > 0)
            {
                ClearSliders();
                for (int i = 0; i < count; i++)
                {
                    AddSlider(new CcSliderNoRowTitle("SLD" + i,0));
                }
            }
        }
        public void AddSlider(CcSliderNoRowTitle slider)
        {
            slider.ClearStatus = ClearSlidersStaus;
            slider.ValueChanged += GetCurrentSlider;           
            int xPos = mSliders.Sum(s => s.Width) + 38;
            Point loc = new Point(xPos, /*this.Location.Y+*/3);
            slider.Location = loc;
            this.Controls.Add(slider);
            mSliders.Add(slider);
            this.Width = slider.Width + xPos;//自动调整宽度
        }
        private void ClearSlidersStaus()
        {
            foreach (var sld in mSliders)
            {
                sld.IsLastInvoked = false;
            }
        }
    }

    //
    //定义ValuesChanged事件，返回当前Slider及当前值
    //
    /// <summary>
    /// 为SliderGroup.ValuesChange事件提供数据
    /// </summary>
    public class ValueChangedEventArgs : EventArgs
    {
        public CcSliderNoRowTitle CurrentSlider { get; set; }
    }
}
